<template>
	<view>
		<view class="p-3 border-top">
			请设置新的手机号
		</view>
		<view class="border-bottom border-top bg-white">
			<view class="border-bottom ml-4 py-2">
				<input type="text" v-model="getphone" placeholder="请输入手机号" placeholder-class="font" @input="getphone_input" />
			</view>
			<view class="ml-4 py-2 d-flex j-sb">
				<input type="text" v-model="code" placeholder="请输入验证码" placeholder-class="font" @input="code_input" />
				<text class="text-danger font mr-5 d-flex a-center" @tap="getCode">{{tips}}</text>
			</view>
		</view>
		<view class="mx-3 mt-5">
			<button type="warn" @click="comfirm">立即更换</button>
		</view>
		<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange">
		</u-verification-code>
		
		<!-- 退出弹出层 -->
		<uni-popup ref="popup" type="center" class="logOutPopup">
			<view class="popup_wd">
				<view class="py-2 bg-danger text-white text-center">提示</view>
				<view class="py-5 text-center bg-white">短信已发送到手机{{getphone}},请注意查收</view>
				<view class="d-flex  bg-light-secondary border-top">
					<view class="flex-1 py-2 text-center border-right" @click="Off">取消</view>
					<view class="flex-1 py-2 text-center" @click="Confirm">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				getphone:"", // 手机号
				code:"", // 验证码
				get_code:"获取验证码",
				tips: '',
				// refCode: null,
				seconds: 120,
			}
		},
		methods: {
			// 手机号
			getphone_input(e){
				this.getphone=e.detail.value
			},
			// 验证码
			code_input(e){
				this.code=e.detail.value
			},
			
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
					if(this.getphone==""){
						uni.showToast({
						    title: '请输入正确的手机号码',
						    duration: 2000
						});
					}else{
						this.$H.post("admin/shop/captcha/sms",{
							type:"user_modify_mobile",
							value:this.getphone
						}).then(res=>{
							console.log(res)
						})
						this.$refs.popup.open()
					}	
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.$u.toast('倒计时结束');
			},
			start() {
				this.$u.toast('倒计时开始');
			},
			// 立即更换
			comfirm(){
				this.$H.post("admin/user/bind",{
					type:"mobile",
					value:this.getphone,
					code:this.code
				}).then(res=>{
					console.log(res)
					uni.navigateBack({
						delta:1
					})
				})
			},
			//退出登录 弹出框 取消
			Off(){
				this.$refs.popup.close()
			},
			//退出登录 弹出框 确认
			Confirm(){
				this.$refs.popup.close()
			}
		}
	}
</script>

<style>

</style>
